/**
 * Created by IntelliJ IDEA.
 * User: ryan
 * Date: Sep 16, 2010
 * Time: 5:48:35 PM
 * To change this template use File | Settings | File Templates.
 */
$(function() {
	module('meetingMinutes');

	test("Init Conditions", 2, function() {
        ok($('#minutes'), 'Div exits to test');
        ok(meeting, 'js object exists');
    });



    test("Meeting with One topic Change", 7, function() {
        var div = $('#minutes');
        var minutes = [
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "type": "topicChange",
                "topic": "Mission_To_Mars",
                "topicStack" : [],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            }
        ];
        meeting.ui.createMeeting('#minutes', minutes);
        equals(div.find('.topicBox').size(),  1, "One Topic Box Exists");
        equals(div.find('.topicBox1').size(),  1, "Lvel One Topic Box");
        equals(div.find('#e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077').size(),  1, "Has correct ID");
        equals(div.find('h2').text(),  "Mission To Mars", "Has correct title");

        equals(div.find('form').attr('id'), 'currentForm', 'Now active with Form');
        equals(div.find('.indentHelper img').attr('title'), 'Leave \'Mission To Mars\' return to \'Meeting With Bob (2010-01-10)\'', 'Indent Icon has proper title based on topics');
        equals(div.find('.createSubtopic img').attr('title'), 'Add subtopic to \'Mission To Mars\'', 'Subtopic button has proper text');

        fixImageReferences();
    });

    test("Meeting with Two topic Changes", 7, function() {
        var div = $('#minutes');
        var minutes = [
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "type": "topicChange",
                "topic": "Mission_To_Mars",
                "topicStack" : [],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            },
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078",
                "type": "topicChange",
                "topic": "Mission_To_Venus",
                "topicStack" : [],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            }
        ];
        meeting.ui.createMeeting('#minutes', minutes);
        equals(div.find('.topicBox').size(),  2, "One Topic Box Exists");
        equals(div.find('.topicBox1').size(),  2, "Lvel One Topic Box");
        var div2 = $('#e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078');
        equals(div2.size(),  1, "Has correct ID");
        equals(div2.find('h2').text(),  "Mission To Venus", "Has correct title");

        equals(div2.find('form').attr('id'), 'currentForm', 'Now active with Form');
        equals(div2.find('.indentHelper img').attr('title'), 'Leave \'Mission To Venus\' return to \'Meeting With Bob (2010-01-10)\'', 'Indent Icon has proper title based on topics');
        equals(div2.find('.createSubtopic img').attr('title'), 'Add subtopic to \'Mission To Venus\'', 'Subtopic button has proper text');

        fixImageReferences();
    });
    test("Meeting with Subtopic Changes", 9, function() {
        var div = $('#minutes');
        var minutes = [
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "type": "topicChange",
                "topic": "Mission_To_Mars",
                "topicStack" : [],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            },
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078",
                "type": "topicChange",
                "topic": "Rocket",
                "parent" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "topicStack" : ["Mission_To_Mars"],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            }
        ];
        meeting.ui.createMeeting('#minutes', minutes);
        equals(div.find('.topicBox').size(),  2, "Two Topic Boxes Exists");
        equals(div.find('.topicBox1').size(),  1, "Lvel One Topic Box");
        equals(div.find('.topicBox2').size(),  1, "Lvel Two Topic Box");
        var div2 = $('#e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078');
        equals(div2.size(),  1, "Has correct ID");
        equals(div2.find('h2').text(),  "Rocket", "Has correct title");

        equals(div2.find('form').attr('id'), 'currentForm', 'Now active with Form');
        equals(div2.find('.indentHelper img').attr('title'), 'Leave \'Rocket\' return to \'Mission To Mars\'', 'Indent Icon has proper title based on topics');
        equals(div2.find('.createSubtopic img').attr('title'), 'Add subtopic to \'Rocket\'', 'Subtopic button has proper text');

        var parent = $('#e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077');
        equals($('.createSubtopic img:last').attr('title'), 'Add subtopic to \'Mission To Mars\'', 'Subtopic button has proper text');



        fixImageReferences();
    });
    
    test("Siblings", 3, function() {
            var div = $('#minutes');
            var minutes = [
                {
                    "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                    "type": "topicChange",
                    "topic": "Mission_To_Mars",
                    "topicStack" : [],
                    "conversation": "Meeting_With_Bob_(2010-01-10)",
                    "recording" : "recording1",
                    "timestamp": 1262325200000,
                    "startTime" : 30,
                    "endTime": 60
                },
                {
                    "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078",
                    "type": "topicChange",
                    "topic": "Rocket",
                    "parent" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                    "topicStack" : ["Mission_To_Mars"],
                    "conversation": "Meeting_With_Bob_(2010-01-10)",
                    "recording" : "recording1",
                    "timestamp": 1262325200000,
                    "startTime" : 30,
                    "endTime": 60
                },
                {
                    "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588079",
                    "type": "topicChange",
                    "topic": "Shuttle",
                    "parent" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                    "topicStack" : ["Mission_To_Mars"],
                    "conversation": "Meeting_With_Bob_(2010-01-10)",
                    "recording" : "recording1",
                    "timestamp": 1262325200000,
                    "startTime" : 30,
                    "endTime": 60
                }
            ];
            meeting.ui.createMeeting('#minutes', minutes);

            equals(div.find('.indentHelper img').attr('title'), 'Leave \'Shuttle\' return to \'Mission To Mars\'', 'Indent Icon has proper title based on topics');
            equals(div.find('.createSubtopic img').attr('title'), 'Add subtopic to \'Shuttle\'', 'Subtopic button has proper text');

            var parent = $('#e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077');
            equals($('.createSubtopic img:last').attr('title'), 'Add subtopic to \'Mission To Mars\'', 'Subtopic button has proper text');

            fixImageReferences();
    });

    test("Reference Items", 1, function() {
        var div = $('#minutes');
        var minutes = [
            {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "type": "topicChange",
                "topic": "Mission_To_Mars",
                "topicStack" : [],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            },
             {
                "_id" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588078",
                "type": "reference",
                "text": "It will be 401000000 km",
                 "parent" : "e409c414-8013-43e5-83e2-29fd64d3182b-ID_1348588077",
                "topicStack" : ["Mission_To_Mars"],
                "conversation": "Meeting_With_Bob_(2010-01-10)",
                "recording" : "recording1",
                "timestamp": 1262325200000,
                "startTime" : 30,
                "endTime": 60
            }
        ];
        meeting.ui.createMeeting('#minutes', minutes);
        equals(div.find('p').text(), 'It will be 401000000 km', 'Has Reference Item');
        fixImageReferences();
    });
    function fixImageReferences() {
         $('#minutes img').each(function() {
             var src = $(this).attr('src');
             src = '../' + src;
            $(this).attr('src', src);
        });
    }

    test("Create Basic Agenda", 2, function() {
        var raw = [
            'Introduction and Welcome',
            'Review Last Minutes',
            'Investment Status',
            'Open Discussion',
            'Next Meeting'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics.length, 5, "Five topics generated");
        equals(topics[0].text, raw[0], "Topic text is expected")
    });
    test("Agenda with topic reference", 2, function() {
        var raw = [
            'Introduction and Welcome',
            'Review Last Minutes',
            'Review [Investment_Status]',
            'Open Discussion',
            'Next Meeting'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics[2].text, "Review Investment Status", "Topic text is expected");
        equals(topics[2].topic, "Investment_Status", "Topic correct");   
    });
    test("Find Topic Depth", 1, function() {
        var item = "\tTest";
        var depth = meeting.findTopicDepth(item);
        equals(depth.depth, 1, "One Tab");
    });
    test("Find Topic Depth, two tabs", 1, function() {
        var item = "\t\tTest";
        var depth = meeting.findTopicDepth(item, 1);
        equals(depth.depth, 2, "Two Tabs Tab");
    });
    test("Find Topic Depth, in []", 1, function() {
        var item = "[Test]";
        var depth = meeting.findTopicDepth(item, 0);
        equals(depth.depth, 0, "Brackets should not contribute to depth");
    });
    test("Find Topic Depth, with -", 1, function() {
        var item = "--Test";
        var depth = meeting.findTopicDepth(item, 1);
        equals(depth.depth, 2, "Dash used as depth");
    });

    test("Agenda with indentation(tabs)", 11, function() {
        var raw = [
            'Introduction and Welcome',
            'Review Last Minutes',
            '[Company_Status]',
            '\tReview [Investment_Status]',
            '\tTalk about [Year_End_2010]',
            'Open Discussion',
            'Next Meeting'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics[2].text, "Company Status", "Topic text is expected");
        equals(topics[2].topic, "Company_Status", "Topic correct");
        equals(topics[3].text, "Review Investment Status", "Topic text is expected");
        equals(topics[3].topic, "Investment_Status", "Topic correct");
        equals(topics[3].topicStack.length, 1, "Topic Stack");
        equals(topics[3].topicStack[0], "Company_Status", "Topic Stack Topic");
        equals(topics[4].text, "Talk about Year End 2010", "Topic text is expected");
        equals(topics[4].topic, "Year_End_2010", "Topic correct");
        equals(topics[4].topicStack.length, 1, "Topic Stack");
        equals(topics[4].topicStack[0], "Company_Status", "Topic Stack Topic");
        equals(topics[5].topicStack.length, 0, "Topic Stack");
    });
    test("Agenda with multi level indentation(--)", 5, function() {
        var raw = [
            'Introduction and Welcome',
            'Review Last Minutes',
            '[Company_Status]',
            '--Review [Investment_Status]',
            '---10 gold pieces',
            '--Talk about [Year_End_2010]',
            '---Taxes an such',
            'Open Discussion',
            'Next Meeting'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics[4].topicStack.length, 2, "Topic Stack");
        equals(topics[4].topicStack[0], "Company_Status", "Topic Stack Topic Level 1");
        equals(topics[4].topicStack[1], "Investment_Status", "Topic Stack Topic Level 2");
        equals(topics[5].topicStack.length, 1, "Topic Stack");
        equals(topics[5].topicStack[0], "Company_Status", "Topic Stack Topic Level 1");

    });
    test("Agenda skip empty lines", 1, function() {
        var raw = [
            'Introduction and Welcome',
            '\n',
            '\t',
            '[Company_Status]'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics.length, 2, "Skip empty lines");
    });

    test("Simple agenda 2", 2, function() {
        var raw = [
            '- Introductions And Welcome',
            '   * Meet [Bob_Alexander]',
            '   * Goodbye [Susie_Poletskiw]',
            '',
            '- Business At Hand',
            '   * Move [Hudson] to a new Home',
            '   * We need a new [Business_Domain_Owner]'
        ];
        var agenda = raw.join('\n');
        var topics = meeting.parseAgenda(agenda);
        equals(topics.length, 6, "Agenda Items");
        equals(topics[0].topicStack.length, 0, "Topic Stack");
    });

})